package sit.engreal.leetcode.problems600;

import org.junit.jupiter.api.Assertions;

/**
 * <a herf="https://leetcode.cn/problems/reverse-words-in-a-string-iii/">557. 反转字符串中的单词 III</a>
 */
public class LeetCode557 {
    public String reverseWords(String s) {
        final StringBuilder builder = new StringBuilder();
        int i = 0;
        int j = 0;
        for (; j < s.length(); j++) {
            if (' ' == s.charAt(j)) {
                for (int k = j - 1; k >= i; k--) {
                    builder.append(s.charAt(k));
                }
                builder.append(' ');
                i = j + 1;
            }
        }
        for (int k = s.length() - 1; k >= i; k--) {
            builder.append(s.charAt(k));
        }

        return builder.toString();
    }

    public static void main(String[] args) {
        final LeetCode557 demo = new LeetCode557();

        Assertions.assertEquals("s'teL ekat edoCteeL tsetnoc", demo.reverseWords("Let's take LeetCode contest"));


        Assertions.assertEquals("doG gniD", demo.reverseWords("God Ding"));
    }
}
